home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.05 May 90 / Line Art Rotation Source / Rotation / rotmainti.c < prev   
Encoding:
C/C++ Source or Header  |  1989-08-14  |  4.6 KB  |  157 lines  |  [TEXT/KAHL]

  1. #include<math.h>
  2. int errno;
  3.  
  4. void mult();  /*out matrix mult proc*/
  5. /*floating value of points to avoid roundoff*/
  6. typedef struct rec {float h,v;} points;
  7. main()
  8. {
  9.   int buttondown=0, /*flagg for mouse       */
  10.       n=-1,         /*number of vertices    */
  11.       keypressed=0, /*flagg for key         */
  12.       flip=0,       /*to allow alternating  */
  13.       flop=1,       /*vertices to be drawn  */
  14.       i;            /*array counter         */
  15.   float x,          /*angle counter         */
  16.       T[3][3],      /*translation matrix    */
  17.       Tinv[3][3],   /*translate back        */
  18.       Rz[3][3],     /*rotate matrix         */
  19.       c[3][3],      /*result of T&R         */
  20.       d[3][3];      /*result of c&Tinv      */
  21.   long curtick,     /*for delay loop        */
  22.        lastick;     /*for delay loop        */
  23.   EventRecord nextevent;/*to get mouse&key  */
  24.   Point origin,dummy;   /*pivot and locator */
  25.   points points[2][30];/*vertices(don’t draw 
  26.                          the Eiffel tower)  */
  27.   WindowPtr scnwdw;    /*window pointer     */
  28.   Rect      scnrect;   /*window rect        */
  29. /*************************************
  30. *  Set things up                     *
  31. *************************************/
  32. InitGraf(&thePort);
  33. InitFonts();
  34. InitWindows();
  35. InitDialogs((Ptr)0L);
  36. TEInit();
  37. InitMenus();
  38. scnrect=screenBits.bounds;
  39. InsetRect(&scnrect,10,25);
  40. scnwdw=NewWindow(0,&scnrect,"\p",TRUE,dBoxProc,
  41.             -1,FALSE,0);
  42. SetPort(scnwdw);
  43. InitCursor();
  44.   
  45. /*************************************
  46. *  Get points                        *
  47. *************************************/
  48.   while(!keypressed)
  49.   {
  50.     buttondown=0;
  51.     SystemTask();
  52.     if(GetNextEvent(-1,&nextevent))
  53.       if(nextevent.what==mouseDown) buttondown=1;
  54.       else if(nextevent.what==keyDown) keypressed=1;
  55.     if(buttondown) /*get a point and draw it*/ 
  56.     {
  57.       GetMouse(&dummy);
  58.       points[0][++n].h=dummy.h;points[0][n].v=dummy.v; 
  59.       if(n==0)
  60.         MoveTo((int)points[0][0].h,(int)points[0][0].v);
  61.       LineTo((int)points[0][n].h,(int)points[0][n].v);
  62.     } /*end of get point*/
  63.   }  /*end of get points*/
  64.   
  65. /*************************************
  66. *  Get origin                        *
  67. *************************************/
  68.   buttondown=0;
  69.   do
  70.   {
  71.     SystemTask();
  72.     if(GetNextEvent(-1,&nextevent))
  73.       if(nextevent.what==mouseDown) buttondown=1;
  74.   }while(!buttondown);
  75.   GetMouse(&origin);
  76.   
  77. /*************************************
  78. *  Make translation matrix           *
  79. *************************************/
  80.   T[0][0]=1;T[0][1]=0;T[0][2]=0;
  81.   T[1][0]=0;T[1][1]=1;T[1][2]=0;
  82.   T[2][0]=-origin.h;T[2][1]=-origin.v;T[2][2]=1;
  83.   Tinv[0][0]=1;Tinv[0][1]=0;Tinv[0][2]=0;
  84.   Tinv[1][0]=0;Tinv[1][1]=1;Tinv[1][2]=0;
  85.   Tinv[2][0]=origin.h;Tinv[2][1]=origin.v;Tinv[2][2]=1;
  86.   Rz[0][2]=0;Rz[1][2]=0;Rz[2][0]=0;Rz[2][1]=0;Rz[2][2]=1;
  87.   
  88. /*************************************
  89. *  Rotate                            *
  90. *************************************/
  91.   x=0.157;  /*rotation angle - about 9 degrees*/
  92.   Rz[0][0]=Rz[1][1]=cos(x);Rz[0][1]=sin(x);
  93.   Rz[1][0]=-Rz[0][1];
  94.   mult(T,Rz,c);
  95.   mult(c,Tinv,d);
  96.   for(x=.157;x<=12.56;x+=0.157)
  97.   {
  98.     flip++;flip=flip%2;flop++;flop=flop%2;
  99.     for(i=0;i<=n;i++)
  100.     {
  101.       points[flip][i].h=points[flop][i].h*d[0][0]
  102.                     +points[flop][i].v*d[1][0]
  103.                     +1*d[2][0];
  104.       points[flip][i].v=points[flop][i].h*d[0][1]
  105.                     +points[flop][i].v*d[1][1]
  106.                     +1*d[2][1];
  107.     }  /*end update points*/
  108.     ForeColor(whiteColor);  /*undraw flop*/
  109.     lastick=TickCount(); /*time delay for retace to improve animation*/
  110.     do{curtick=TickCount();} while(lastick+1>curtick);
  111.     MoveTo((int)points[flop][0].h,(int)points[flop][0].v);
  112.     for(i=1;i<=n;i++) LineTo((int)points[flop][i].h,(int)points[flop][i].v);
  113.     ForeColor(blackColor);  /*draw flip*/
  114.     lastick=TickCount();    
  115.     do{curtick=TickCount();} while(lastick+1>curtick);
  116.     MoveTo((int)points[flip][0].h,(int)points[flip][0].v);
  117.     for(i=1;i<=n;i++) LineTo((int)points[flip][i].h,(int)points[flip][i].v);
  118.   }  /*end rotate*/
  119.     
  120. /*************************************
  121. *  End everything                    *
  122. *************************************/
  123.   buttondown=0;
  124.   do
  125.   {
  126.     SystemTask();
  127.     if(GetNextEvent(-1,&nextevent))
  128.       if(nextevent.what==mouseDown) buttondown=1;
  129.   }while(!buttondown);
  130.  
  131. DisposeWindow(scnwdw);
  132.  
  133. }  /*program end*/
  134.  
  135. void mult(A,B,C)
  136.   float A[][3],B[][3],C[][3];
  137. {
  138.   int i,j,k;
  139.   
  140.   for(i=0;i<=2;i++)
  141.     for(j=0;j<=2;j++)
  142.     {
  143.       C[i][j]=0.0;
  144.       for(k=0;k<=2;k++)
  145.         C[i][j]+=A[i][k]*B[k][j];
  146.     }
  147. }  /*end mult*/
  148.       
  149.       
  150.       
  151.       
  152.       
  153.       
  154.       
  155.       
  156.       
  157.